<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML><html>
  <head>
    <base href="<%=basePath%>">
    
    <title>商品进货单  - 仓库管理</title>
	<script type="text/javascript" src="<%=basePath%>/comm/js/map.js"></script>
	<link href="<%=basePath %>/background/css/layout.css" rel="stylesheet" type="text/css" />
    <link href="<%=basePath %>/background/css/header.css" rel="stylesheet" type="text/css" />
    <link href="<%=basePath %>/warehouse/css/button.css" rel="stylesheet" type="text/css" />
  </head>
 
  <body>
  
  <div id="container">
  <div id="header">
		 <jsp:include page="/background/head.jsp"></jsp:include>
	</div>
  <div id="applicationbar">
		 <jsp:include page="/background/app/appbar.jsp"></jsp:include>
	</div>
  <div id="mainContent">
    <div id="sidebar">

		<jsp:include page="/background/left.jsp"></jsp:include>
	</div>
	
	
	
    <div id="content" >
    <div style="border:1px solid #ffffff; border-top-color:#6cb1f1; background-color: #e5f3ff; width: 100%; top: 0px;" >
							<a href="<%=basePath %>warehouse/managepurchaselist_warehouse.action">进仓管理</a>  - 商品进货单
     			
     			<input type="button" value="查看谁有权限" class="small button orange"
     				onclick="javascript:openUrlForDialog('selectoperatorlimitedAction_background.action?limitedCode=<s:property value="limitedcode"/>')"/>
	</div>
	
	<label>
		<s:fielderror id="errormessage" />
	</label>
    
    <form action="warehouse/addpurchaselist_warehouse.action" method="post" accept-charset="UTF-8" id="addpurchaselist_warehouseForm" name="addpurchaselist_warehouseForm"> 
				<s:token />
     <table style="width:100%">
    	
 		<tr>
     		<td>
     			<FONT color="red"><s:property value="operationResult"/></FONT>
     			<s:fielderror id="errormessage" />
     			<br/>
     			
				<table  style= "border:1px solid #ffffff; width: 100%;" class="bottomline">
					<tr>
					
						<td width="100">
						
						
						
						</td>
						<td  width="300">
						
						</td>
					</tr>
					
					<tr>
					
						<td>供货商名称:</td>
						<td>
						
						<input type="text" class="searchtxt" id="supplier_purchaselist" name="supplier_purchaselist" size="40" maxlength="32" autofocus="autofocus" required="required"/>
						
						
						</td>
						<td align="left">
							<input class="large button blue" style="bottom: 2px;" type="button" value="选择供应商" onclick="javascript:openUrlForDialog('insertsupplierinfo_warehouse.action'); "/>
						</td>
						<td></td>
					</tr> 
					
					<tr>
					
						<td width="100">供货商信息:</td>
						<td >
						
						
						<textarea id="supplierinfo_purchaselist" name="supplierinfo_purchaselist" rows="3" cols="39"></textarea>
						
						</td>
						<td >
						
						</td>
						<td></td>
					</tr>
					
					
				<tr>	
					<td>
								  仓库:
								  
							  </td>
				  			<td id="warehousedisply">
				  				<select id="warehouseid_purchaselist" name="warehouseid_purchaselist">
				  					<s:iterator value="warehouseList" id="warehouseList">
				  						
				  						<option value="<s:property value="#warehouseList.id_warehouse"/>"><s:property value="#warehouseList.name_warehouse"/></option>	
				  					</s:iterator>
								  	
								  	
								  </select>
				  			</td>
				  			<td >
						
						</td>
						<td></td>
						
				  		</tr>
				</table>
				

	</td>
     		</tr>
     		
     		<tr>
				<td>
					<table id="receiptListTable" width="100%" border="0"   cellpadding="2"  bordercolor="#6cb1f1" cellspacing="0" style= "border:1px solid #ffffff;border-top-color:#6cb1f1;border-bottom-color:#6cb1f1;">
								<thead>
								<tr bgcolor="#e5f3ff">
									
									<td width="110">
									     商品ID
									</td>
									<td width="305">
										商品名称
									</td>
									
									<td width="105">
										数量(单位)
									</td>
								
									<td width="85">
										单价
									</td>
									
									<td width="85">
										全额
									</td>
									
									
									<td>
										备注
									</td>
									
									<td width="50">
										操作
									</td>
								</tr>
								</thead>
								<tbody>
								</tbody>
								
							</table>
				</td>
     		</tr>
     		 <tr>
     		<td>
     			<table>
     				<tr>
     					<td>
     						<input class="mtxt" type="text" size="20" id="searchStr" name="searchStr" onkeydown="javascript:insertPurchaseEvt(this);"/>
     						<input style="bottom: 3px" class="large button blue" value="添加产品" type="button" onclick="javascript:openUrlForDialog('insertpurchaseinfo_warehouse.action?searchStr='+$('#searchStr').attr('value'));"/>
     					</td>
     				</tr>
     				
     			</table>
     		</td>
     		</tr>
     		 <tr>
     		<td>
     		
     		
     			<table>
     			
     			<tr>
					
						<td>运费:</td>
						<td >
							<input class="mtxt" id="shipprice_purchaselist" name="shipprice_purchaselist" onchange="enterShip()"/>
						</td>
						<td>产品数量:</td>
						<td id="goodsnumber">0</td>
						
						<td>总额:</td>
						<td  id="totalDisply">0</td>
					</tr>
					
					
					<tr>
						<td>描述/备注:</td>
						<td>
						<input type="hidden" id="shipprice_purchaselist" name="shipprice_purchaselist" value="0.00"/>
						<input type="hidden" id="goodsnumber_purchaselist" name="goodsnumber_purchaselist" value="0"/>
						<input type="hidden" id="total_purchaselist" name="total_purchaselist" value="0"/>
						<input type="hidden" id="purchaseInfo" name="purchaseInfo" value=""/>
						
						<input type="text" id="remark_purchaselist" name="remark_purchaselist"/>
						
						</td>
						<td></td>
						<td></td>
						<td></td>
						<td></td>
					</tr>
					

				</table>
		
     	</td>
     	</tr>
     		
     		 <tr>
     		<td>
     			<button class="large button yellow" type="button" onclick="submitReceiptlist()">添加</button>
		
     	</td>
     	</tr>
     	
     	
     	
     	</table>
     	</form>
   
    

	
	</div>
  </div>
  <div id="footer">
<jsp:include page="/background/tail.jsp"></jsp:include>
	</div>
</div>
  
  
  </body>
  <script type="text/javascript">

  var number = 0;
  var total = 0.00;
  var shipprice = 0;
  var infoMap = new Map();

  var mapId = 0;
  var goodsreserve = 0;
  function submitReceiptlist(){
	  enterShip();
	  addpurchaselist_warehouseForm['shipprice_purchaselist'].value = shipprice;
	  addpurchaselist_warehouseForm['goodsnumber_purchaselist'].value = number;
	  addpurchaselist_warehouseForm['total_purchaselist'].value = total + shipprice;

	  var supplier = addpurchaselist_warehouseForm['supplier_purchaselist'].value;

	  if(supplier == ""){
			
			alert("供应商不能为空");
			return ;
		}

	  if(infoMap.elements.length<1){
			alert("请选择需要进货的商品, :) ");
			return ;
		}
	
		

			
	 var infoValue = "";

	 infoValue = ( $("#receiptListTable input").map(function(){
		  return $(this).val();
		}).get().join(",") );

	addpurchaselist_warehouseForm['purchaseInfo'].value = infoValue;
	  
	 addpurchaselist_warehouseForm.submit();
  }
  
  function insertGoodsForTable(goodsinfo){
	  
		//alert(goodsinfo);
		var goodsinfovalue = goodsinfo;
		if(goodsinfo != undefined){
			//addpurchase_warehouseForm["goodsinfo"].value=goodsinfo;
			
			
			index = goodsinfo.indexOf(",");
			var goodsid = goodsinfo.substring(0,index);

			goodsinfo = goodsinfo.substring(index+1,goodsinfo.length);
			index = goodsinfo.indexOf(",");
			var goodsname = goodsinfo.substring(0,index);


			goodsinfo = goodsinfo.substring(index+1,goodsinfo.length);
			index = goodsinfo.indexOf(",");
			var goodsnumber = new Number(goodsinfo.substring(0,index));

			goodsinfo = goodsinfo.substring(index+1,goodsinfo.length);
			index = goodsinfo.indexOf(",");
			var goodsunit = goodsinfo.substring(0,index);

			goodsinfo = goodsinfo.substring(index+1,goodsinfo.length);
			index = goodsinfo.indexOf(",");
			var goodsprice = new Number(goodsinfo.substring(0,index));

			goodsinfo = goodsinfo.substring(index+1,goodsinfo.length);
			index = goodsinfo.indexOf(",");
			var remark = goodsinfo.substring(index,goodsinfo.length);

			

			var count = goodsprice * goodsnumber;
			
			


			
			number += goodsnumber;
			total += count;
			
			shipprice = new Number(document.getElementById("shipprice_purchaselist").value);
			//alert(shipprice);
			document.getElementById("goodsnumber").innerHTML = number;
			document.getElementById("totalDisply").innerHTML = (total+shipprice) + " <font color='red'>(包含邮费:" + shipprice + ")<font>";
			
			addRow(goodsid,goodsname,goodsnumber,goodsunit,goodsprice,count,remark,goodsinfovalue);
			
			
		}
		 
	};


	function deleteRow(r,goodsnumber,count,id)   
	{   
		var i=r.parentNode.parentNode.rowIndex;
		document.getElementById('receiptListTable').deleteRow(i);
		number -= goodsnumber;
		total -= count;
		
		shipprice = new Number(document.getElementById("shipprice_purchaselist").value);
		//alert(shipprice);
		document.getElementById("goodsnumber").innerHTML = number;
		document.getElementById("totalDisply").innerHTML = (total+shipprice) + " <font color='red'>(包含邮费:" + shipprice + ")<font>";
		infoMap.remove(id);
	}   
	  
	

	function enterShip(){

		
		shipprice = new Number(document.getElementById("shipprice_purchaselist").value);


		if(isNaN(shipprice)){
			//alert("adsf");
			shipprice = 0 ;
			document.getElementById("shipprice_purchaselist").value=0;
		}
		/*alert(shipprice);
		if(isNaN(shipprice)){
			shipprice = new Number(0.0);
		}//*/
		
		document.getElementById("totalDisply").innerHTML = (total+shipprice) + " <font color='red'>(包含邮费:" + shipprice + ")<font>";
	}
	function insertSupplier(supplierinfo){
		  
		//alert(goodsname);
		//alert(supplierinfo);
		if(supplierinfo != undefined){
			//addpurchaselist_warehouseForm["supplierinfo"].value=supplierinfo;
		
			var index = supplierinfo.indexOf(",");
			var supplier = supplierinfo.substring(0,index);
			
			//alert(goodname);
			addpurchaselist_warehouseForm["supplier_purchaselist"].value=supplier;
			addpurchaselist_warehouseForm["supplierinfo_purchaselist"].value=supplierinfo;
		}

		$("#dialog").dialog('close');
		// opener.
		 
	};
    
	function insertPurchaseEvt(evt){
  		evt = window.event || evt;
  		keyCode = evt.keyCode;
  		if(keyCode==13){//如果取到的键值是回车
  			//alert("OK" + evt.keyCode);
  			javascript:openUrlForDialog('insertpurchaseinfo_warehouse.action?searchStr='+$('#searchStr').attr('value'));
  			return ;
  		}
  		return ;
  	}
  	

  var goodsreserve = 0;
  var goodsId = 0;
  	function choseGoods(goodsid,goodsname,goodsunit){
  		var goodsinfo = "";
  		//,号异常处理
  		goodsname = goodsname.replace(","," ");
  		goodsunit = goodsunit.replace(","," ");

  	  	  	  	
    	goodsinfo += goodsid + ",";
    	goodsinfo += goodsname + ",";
    		
    	goodsinfo +=  "1,";
    	goodsinfo += goodsunit + ",";
    	goodsinfo +=   "0.0,";
    	goodsinfo +=  " ";
    		
    	insertGoodsForTable(goodsinfo);
    	$("#dialog").dialog('close');
	}

  	function changeGoodsInfo(mapId){
  	  	
  	
  		
  		var tGoodsId = document.getElementById("tGoodsId"+mapId).value;
  		var tGoodsName = document.getElementById("tGoodsName"+mapId).value;
  		var tGoodsNumber = document.getElementById("tGoodsNumber"+mapId).value;
  		var tGoodsUnit = document.getElementById("tGoodsUnit"+mapId).value;
  		var tGoodsPrice = document.getElementById("tGoodsPrice"+mapId).value;
  		var tRemark = " ";

  		 

  		//checkGoodsReserve(tWarehouseId,tGoodsId,tGoodsNumber,tGoodsPrice,mapId);
  		 
  		
  	  	
  	// var url=encodeURI(encodeURI("getprofit_warehouse.action?warehouseid="+tWarehouseId + "&goodsid="+tGoodsId + "&number="+tGoodsNumber));
  	 
  	  	
  		if(!isInteger(tGoodsNumber)){
			//alert("数量 只能输入整数和不能为空!");
			document.getElementById("tGoodsNumber"+mapId).value = 1;
			document.getElementById("tGoodsNumber"+mapId).focus();
			//return;
	  	}
	  	if(tGoodsNumber<=0){
	  		//alert("数量 不能少于1,确认后输入");
	  		document.getElementById("tGoodsNumber"+mapId).value = 1;
			document.getElementById("tGoodsNumber"+mapId).focus();
			
		 }
		
	  	if(tGoodsPrice==""){
	  		//alert("单价 只能输入数字和不能为空!");
	  		document.getElementById("tGoodsPrice"+mapId).value = 0.0;
	  		document.getElementById("tGoodsPrice"+mapId).focus();
	  		//return;
		}
		
		//alert(price);
  	  	if(isNaN(tGoodsPrice)){
			//alert("单价 只能输入数字和不能为空!");
			document.getElementById("tGoodsPrice"+mapId).value = 0.0;
	  		document.getElementById("tGoodsPrice"+mapId).focus();
			//return;
  	  	}
  		
  		var goodsinfo =
  		 tGoodsId + ","
  		+ tGoodsName + ","
  		+ tGoodsNumber + ","
  		+ tGoodsUnit + ","
  		+ tGoodsPrice + ","
  		+ tRemark ;

  		//alert(mapId);
  		document.getElementById("tCount"+mapId).innerHTML=tGoodsNumber * tGoodsPrice;
  		
  		//alert(goodsinfo);
  		infoMap.remove(mapId)
  		infoMap.put(mapId,goodsinfo);

  		changeTotalInfo();
  		
  		
  		
		
		//alert(mapId + "," + goodsNumber.value + "," + currentGoodsNumber);
  	 }

  	function changeTotalInfo(){
  		
  		shipprice = new Number(document.getElementById("shipprice_purchaselist").value);
  	  	var totalvalue = 0;
  	  	var goodstotalnumber = 0;
  		for(i=0;i<infoMap.elements.length;i++){
			var mapKey = infoMap.elements[i].key;
			var goodsNumber = document.getElementById("tGoodsNumber"+mapKey).value;
			var goodsPrice = document.getElementById("tGoodsPrice"+mapKey).value;
			totalvalue += goodsNumber*goodsPrice;
			goodstotalnumber += Number(goodsNumber);
		}//*/
		
		document.getElementById("goodsnumber").innerHTML = goodstotalnumber;
		//shipprice = new Number(document.getElementById("shipprice_purchaselist").value);
		total = totalvalue;
		number = goodstotalnumber;
		
		document.getElementById("totalDisply").innerHTML = (totalvalue+shipprice) + " <font color='red'>(包含邮费:" + shipprice + ")<font>";
  	 }
  
  	function insertGoods(){
  	  	var goodsinfo = "";

		
		
		
		//alert(document.getElementById("number").value);
		
		document.getElementById("number").value = document.getElementById("number").value.trim();
		document.getElementById("price").value = document.getElementById("price").value.trim();
		
		
		var number = document.getElementById("number").value.trim();
		var price = document.getElementById("price").value.trim();
		var remark = document.getElementById("remark").value.trim();

		
		if(!isInteger(number)){
			alert("数量只能输入整数和不能为空!");
			document.getElementById("number").focus();
			return;
	  	}
	  	if(number<=0){
	  		alert("数量不能少于1,确认后输入");
			document.getElementById("number").focus();
			return;
		 }
		
	  	if(price==""){
	  		alert("单价只能输入数字和不能为空!");
	  		document.getElementById("price").focus();
	  		return;
		}
		
		//alert(price);
  	  	if(isNaN(price)){
			alert("单价只能输入数字和不能为空!");
			document.getElementById("price").focus();
			return;
  	  	}
		var goodsid = document.getElementById("goodsid").value;
  	  //	getGoodsNumber(warehouseid,goodsid);

  	  //	if(goodsreserve <number){
		//	alert("库存不足");
	//		return;
  	//  	 }
  	  	//goodsinfo += document.getElementById("warehouse").value + ",";
  	  	//alert(document.getElementById("warehouse").value);
	  	  	  	
  		goodsinfo += goodsid + ",";
  		goodsinfo += document.getElementById("goodsname").value + ",";
  		
  		goodsinfo +=  number + ",";
  		goodsinfo += document.getElementById("goodsunit").value + ",";
  		goodsinfo +=  price + ",";
  		goodsinfo +=  remark + " ";
  		
  		insertGoodsForTable(goodsinfo);
  		$("#dialog").dialog('close');
  	}

  	function createXmlHttp() {
  	    //根据window.XMLHttpRequest对象是否存在使用不同的创建方式
  	    if (window.XMLHttpRequest) {
  	       xmlHttp = new XMLHttpRequest();                                     //FireFox、Opera等浏览器支持的创建方式
  	    } else {
  	       xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");   //IE浏览器支持的创建方式
  	    }
  	  
  	    return xmlHttp;
  	}

  
  	function getGoodsSellInfo(){
  		xmlhttp = null;    
  		xmlhttp = createXmlHttp();
  		url = encodeURI(encodeURI("getgoodssellinfo_warehouse.action?goodsid=" + goodsId));
  		//alert(url);
  		xmlhttp.open("POST", url, true);
  		xmlhttp.onreadystatechange = insertGoodsSellInfo;    
  		xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");    
  		xmlhttp.send(null);	
  	}
  	function insertGoodsSellInfo(){
  		if (xmlhttp.readyState == 4) {  
		      if (xmlhttp.status == 200) {    
		       //	var saerchResult = "{\"options\":\"" + xmlhttp.responseText +"\"}" ;    
		       var replyResult =  xmlhttp.responseText;  
		      // alert(replyResult);
		       var index = replyResult.indexOf(",");
				var number = replyResult.substring(0,index);
				replyResult = replyResult.substring(index+1,replyResult.length);
			//	alert(replyResult);
				//var price = replyResult.substring(index-1,replyResult.length);
				var price = replyResult;
		     	document.getElementById('number').value = number;
		     	document.getElementById('price').value = price;
		     
		      }    
		  }
  	}

  	function insertWarehouseInfo(){
  		if (xmlhttp.readyState == 4) {  
  		      if (xmlhttp.status == 200) {    
  		       //	var saerchResult = "{\"options\":\"" + xmlhttp.responseText +"\"}" ;    
  		       var replyResult =  xmlhttp.responseText;  
  		       //alert(replyResult);
  		     document.getElementById('warehousedisply').innerHTML = replyResult;
  		      }    
  		   }
  		
  	}

	function getGoodsNumber(warehouseid,goodsid){
  		
  		xmlhttp = null;    
  		xmlhttp = createXmlHttp();
  		url = encodeURI(encodeURI("getgoodsnumber_warehouse.action?warehouseid="+warehouseid+"&goodsid=" + goodsid));
  		//alert(url);
  		xmlhttp.open("POST", url, true);
  		xmlhttp.onreadystatechange = setCheckGoodsNumberInfo;    
  		xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");    
  		xmlhttp.send(null);	
  		
  		
  	}

  	function setCheckGoodsNumberInfo(){
  		if (xmlhttp.readyState == 4) {  
  		      if (xmlhttp.status == 200) {    
  		       //	var saerchResult = "{\"options\":\"" + xmlhttp.responseText +"\"}" ;    
  		       goodsreserve =  xmlhttp.responseText;
  		       //alert(xmlhttp.responseText);
  		       //alert(replyResult);
  		     //document.getElementById('warehousedisply').innerHTML = replyResult;
  		      }    
  		   }
  		
  	}
  	
  	function isInteger(str) {
  		var regu=/^[-]{0,1}[0-9]{1,}$/;
		return regu.test(str);
	} 
  	/**  
  	*删除左右两端的空格  
  	*/  
  	String.prototype.trim=function(){      
  	    return this.replace(/(^\s*)|(\s*$)/g, '');   
  	};
  	String.prototype.replaceAll = function(s1,s2){
  		return this.replace(new RegExp(s1,"gm"),s2);
  	};
		

  	$("#receiptListTable tbody tr").hover(function(){ 
  	    $(this).children("td").addClass("hover");
  	},function(){ 
  	    $(this).children("td").removeClass("hover");
  	});

  	//客户端动态添加行
  	function addRow(goodsid,goodsname,goodsnumber,goodsunit,goodsprice,count,remark,goodsinfovalue){   
		
		mapId++;
		
		//alert(i);
		//newRow.insertCell().innerHTML = warehousename;   
		//newRow.insertCell().innerHTML = trcode;   
		var htmlStr  = "";
		htmlStr = htmlStr + "<td>"+ goodsid + "<input id='tGoodsId"+mapId+"' name='tGoodsId"+mapId+"'  type='hidden' value='"+ goodsid +"'/>"
		+ "</d>";
		htmlStr = htmlStr +  "<td>"+ "<a href=\"#\">"+goodsname + "</a>"
		+ "<input id='tGoodsName"+mapId+"' name='tGoodsName"+mapId+"'  type='hidden' value='"+ goodsname +"'/>"+ "</d>";

		
		
		htmlStr = htmlStr +"<td>"+ "<input id='tGoodsNumber"+mapId+"' name='tGoodsNumber"+mapId+"' onblur='changeGoodsInfo("+mapId+")' size='4' type='text' value='"+ goodsnumber +"'/>(" + goodsunit + ")"
		+ "<input id='tGoodsUnit"+mapId+"' name='tGoodsUnit"+mapId+"'  type='hidden' value='"+ goodsunit +"'/>"+ "</d>";


		
		htmlStr = htmlStr +"<td>"+ "<input id='tGoodsPrice"+mapId+"' name='tGoodsPrice"+mapId+"' onblur='changeGoodsInfo("+mapId+")' size='6' type='text' value='"+ goodsprice +"'/>"+ "</d>";
		htmlStr = htmlStr +"<td>"+ "<div id='tCount"+mapId+"' name='tCount"+mapId+"'>"+count+"</div>"+ "</d>";
		htmlStr = htmlStr +"<td>"+ "<input id='tRemark"+mapId+"' name='tRemark"+mapId+"'  type='hidden' value='"+remark+" '/>"+ "</d>";
	
		//alert("mapId"+mapId);
		htmlStr = htmlStr +"<td>"+ "<input id='tSplit"+mapId+"' name='tSplit"+mapId+"'  type='hidden' value='|'/>"
		+ "<a href='javascript:deleteRow(this,"+goodsnumber+","+count+","+mapId+");' onclick='deleteRow(this,"+goodsnumber+","+count+","+mapId+")'>删除</a>"+ "</d>"; 

		htmlStr = "<tr>"+ htmlStr + "</tr>";
		infoMap.put(mapId,goodsinfovalue);
		
  	    //行号是从0开始，最后一行是新增、删除、保存按钮行 故减去2
  	   // var rownum=$("#receiptListTable tbody tr").length;
		//alert(htmlStr + rownum);

		var tb = $("#receiptListTable>tbody");
        tb.append(htmlStr);
	
		
  	  //  $(htmlStr).insertAfter($("#receiptListTable tbody tr:eq("+rownum+")"));   
  	}
  </script>
  
</html>
